class GameUI2 extends egret.Sprite {

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }
    private ycx1;//游泳线
    private ycx2;
    private ycx3;
    private way1;//路径
    private way2;
    private mc//选手
    private left_arrows;//点击按钮
    private right_arrows;
    private speed = 5;//游泳速度
    private win = false//游戏输赢
    private way = 0;
    private countdownMaxNumber = 0;
    private countdownCurNumber;
    private countdownSprite;//倒计时显示对象
    private scoreSprite;//分数时显示对象
    private frameTime;
    private scoreText;
    private floor;//地面
    private swinMp3_bl = true;
    private mcFactory;
    private timer;
    private bgBit;//水
    private bgBit2;//水
    //音乐
    private du_wav;
    private dd;
    private qiang;
    private cheer;
    private tiaoshui;
    private poshui1;
    private poshui2;
    private createView(): void {
        //设置当前场景的锚点
        this.x = 320;
        this.y = 568;
        this.anchorOffsetX = 320;
        this.anchorOffsetY = 568;
        //加载声音
        var swinMp3 = new egret.Sound;
        swinMp3.load("resource/assets/swin2.mp3");
        //du
        this.du_wav = new egret.Sound;
        this.du_wav.load("resource/assets/du.wav");
        //dd
        this.dd = new egret.Sound;
        this.dd.load("resource/assets/dd.mp3");
        //枪声
        this.qiang = new egret.Sound;
        this.qiang.load("resource/assets/qiang.mp3");
        //欢呼
        this.cheer = new egret.Sound;
        this.cheer.load("resource/assets/cheer.mp3");
        //跳水
        this.tiaoshui = new egret.Sound;
        this.tiaoshui.load("resource/assets/tiaoshui.mp3");
        //泼水1
        this.poshui1 = new egret.Sound;
        this.poshui1.load("resource/assets/poshui1.mp3");
        //泼水2
        this.poshui2 = new egret.Sound;
        this.poshui2.load("resource/assets/poshui2.wav");

        //游泳线
        this.ycx1 = createBitmap("ycx1_png", 292, 230);
        this.ycx2 = createBitmap("ycx2_png", 292, -600);
        this.ycx3 = createBitmap("ycx2_png", 292, 0);
        this.addChild(this.ycx1);
        this.addChild(this.ycx2);
        this.addChild(this.ycx3);
        this.ycx3.alpha = 0;

        //路径
        this.way1 = createBitmap("way_png", 94, 0);
        this.way2 = createBitmap("way_png", 94, -1136);
        this.addChild(this.way1);
        this.addChild(this.way2);
        //游泳选手
        this.mcFactory = new egret.MovieClipDataFactory(RES.getRes("swin_json"), RES.getRes("swin_png"));
        this.mc = new egret.MovieClip(this.mcFactory.generateMovieClipData("ready"));
        this.mc.x = (640 - this.mc.width) * .5;
        this.mc.y = 1136 - this.mc.height + 180;
        //把人物添加到舞台
        this.addChild(this.mc);

        //水
        this.bgBit = createBitmap("indexBg_png");
        this.addChild(this.bgBit);

        this.bgBit.anchorOffsetX = 320;
        this.bgBit.x = 320;
        this.bgBit.alpha = .8;

        egret.Tween.get(this.bgBit, { loop: true }).to({ scaleX: 3 }, 5000).to({ scaleX: 1 }, 5000);


        //水2
        this.bgBit2 = createBitmap("indexBg_png");
        this.addChild(this.bgBit2);

        this.bgBit2.anchorOffsetX = 320;
        this.bgBit2.x = 320;
        this.bgBit2.y = -1136;
        this.bgBit2.alpha = .8;

        egret.Tween.get(this.bgBit, { loop: true }).to({ scaleX: 3 }, 5000).to({ scaleX: 1 }, 5000);


        //添加头像容器（头像）
        var imgDisplay = new egret.DisplayObjectContainer();
        imgDisplay.width = 138;
        imgDisplay.height = 138;
        imgDisplay.x = 42;
        imgDisplay.y = 32;
        this.addChild(imgDisplay);
        var imgBox = new egret.Shape();
        imgBox.graphics.beginFill(0xffffff);
        imgBox.graphics.drawRoundRect(0, 0, 138, 138, 138, 138);
        imgBox.graphics.endFill;
        imgDisplay.addChild(imgBox);
        MYajax({ "type": "userinfo" }, "http://swin.bluej.cn/index.php/Home/app/get_userinfo.html", function (res) {
            //console.log(res)
            RES.getResByUrl(res.data.image, function (texture: egret.Texture): void {
                var result = new egret.Bitmap();
                result.texture = texture;
                result.width = 138;
                result.height = 138;
                result.x = 0;
                result.y = 0;
                imgDisplay.addChild(result);
                result.mask = imgBox;
            }, this, RES.ResourceItem.TYPE_IMAGE);
        })
        //游泳按钮

        this.left_arrows = createBitmap("left_arrows_png", 50, 914);
        this.addChild(this.left_arrows);
        egret.Tween.get(this.left_arrows, { loop: true }).to({ x: this.left_arrows.x - 30 }, 1000);
        this.right_arrows = createBitmap("right_arrows_png", 350, 914);
        this.addChild(this.right_arrows);
        egret.Tween.get(this.right_arrows, { loop: true }).to({ x: this.right_arrows.x + 30 }, 1000);
        //点击左右文字
        var oiText = createTextFiled("点击左右,向前游动", 0, 1058, 32, 0xffffff, "center", 640);
        this.addChild(oiText);

        //地面
        this.floor = createBitmap("floor_png", 0, -310);
        this.addChild(this.floor);
        //倒计时
        //添加倒计时
        this.frameTime = 1 / this.$stage.frameRate;
        var countdownText = createTextFiled("Time", 297, 32, 18, 0xffffff);
        this.addChild(countdownText);
        this.countdownCurNumber = this.countdownMaxNumber;
        this.countdownSprite = createTextFiled(this.countdownMaxNumber.toString(), 0, 50, 40, 0xffffff, "center", 640);
        this.addChild(this.countdownSprite);
        //3-2-1-go
        var readyGo = createTextFiled("3", 0, 300, 146, 0xfff434);
        readyGo.bold = true;
        this.addChild(readyGo);
        this.scoreText = readyGo;
        readyGo.x = (640 - readyGo.width) * .5 + 50;
        readyGo.anchorOffsetX = readyGo.width * .5;
        readyGo.anchorOffsetY = readyGo.height * .5;
        readyGo.scaleX = 0;
        readyGo.scaleY = 0;

        var This=this;
        this.mc.touchEnabled = true;

        this.mc.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            if (this.win == false) {
                this.poshui1.play(0, 1);

                if (this.speed >= 40) {
                    this.speed = 40;
                    this.mc.frameRate = this.mc.frameRate;
                } else {
                    this.speed += 5;
                    this.mc.frameRate += 2;
                }

                this.mc.movieClipData = this.mcFactory.generateMovieClipData("swin")
                this.mc.play(-1);

            }
        }, this);

        //减速
        setInterval(function () {
            if (this.speed <= 0) {
                this.speed = 0;
                // this.mc.movieClipData = this.mcFactory.generateMovieClipData("ready");
                this.mc.frameRate = 16;
            } else {
                this.speed -= 5;
                this.mc.play(1);
                this.mc.frameRate--;
            }
            if (this.speed >= 8) {
                this.cheer.play(0, 1);
            }
        }.bind(this), 400);





        this.timer = new egret.Timer(100, 0);
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.gameStar, this);
        this.timer.start();
    }

    //判断游戏开始
    private gameStar() {
        if (Main.game_bl) {
            Main.game_bl = false;
            egret.Tween.get(this.scoreText).to({ scaleX: 1, scaleY: 1 }, 600).call(function () { this.du_wav.play(0, 1) }.bind(this)).to({ scaleX: 0, scaleY: 0 }, 600).call(function () {

                this.scoreText.text = "2";
                this.scoreText.anchorOffsetX = this.scoreText.width * .5;
                this.scoreText.anchorOffsetY = this.scoreText.height * .5;
                egret.Tween.get(this.scoreText).to({ scaleX: 1, scaleY: 1 }, 600).call(function () { this.du_wav.play(0, 1) }.bind(this)).to({ scaleX: 0, scaleY: 0 }, 600).call(function () {
                    this.scoreText.text = "1";
                    this.scoreText.anchorOffsetX = this.scoreText.width * .5;
                    this.scoreText.anchorOffsetY = this.scoreText.height * .5;
                    egret.Tween.get(this.scoreText).to({ scaleX: 1, scaleY: 1 }, 600).call(function () { this.du_wav.play(0, 1) }.bind(this)).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                        this.scoreText.text = "GO!";
                        this.scoreText.anchorOffsetX = this.scoreText.width * .5;
                        this.scoreText.anchorOffsetY = this.scoreText.height * .5;

                        egret.Tween.get(this.scoreText).to({ scaleX: 1, scaleY: 1 }, 100).call(function () { this.qiang.play(0, 1); }.bind(this)).to({ scaleX: 0, scaleY: 0 }, 500).call(function () {
                            //游戏主循环，可以给当前场景添加一个“进入帧”事件
                            this.tiaoshui.play(0, 1);
                            this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);

                            egret.Tween.get(this.mc).to({ y: 250 }, 300).call(function () {
                                egret.Tween.get(this.ycx1).to({ y: 460 }, 300);
                                egret.Tween.get(this.ycx2).to({ y: -350 }, 300);



                                //点击-左

                                this.left_arrows.touchEnabled = true;
                                this.left_arrows.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {

                                    if (this.win == false) {
                                        this.poshui1.play(0, 1);
                                        this.left_arrows.touchEnabled = false;
                                        this.right_arrows.touchEnabled = true;
                                        if (this.speed >= 20) {
                                            this.speed = 20;
                                            this.mc.frameRate = this.mc.frameRate;
                                        } else {
                                            this.speed += 5;
                                            this.mc.frameRate += 2;
                                        }

                                        this.mc.movieClipData = this.mcFactory.generateMovieClipData("swin")
                                        this.mc.play(-1);

                                    }
                                }.bind(this))

                                //点击-右
                                this.right_arrows.touchEnabled = true;
                                this.right_arrows.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
                                    if (this.win == false) {
                                        this.poshui2.play(0, 1);
                                        this.right_arrows.touchEnabled = false;
                                        this.left_arrows.touchEnabled = true;
                                        if (this.speed >= 20) {
                                            this.speed = 20;
                                            this.mc.frameRate = this.mc.frameRate;
                                        } else {
                                            this.speed += 5;
                                            this.mc.frameRate += 2;
                                        }

                                        this.mc.movieClipData = this.mcFactory.generateMovieClipData("swin")
                                        this.mc.play(-1);

                                    }
                                }.bind(this));



                            }.bind(this));

                        }.bind(this));
                    }.bind(this));
                }.bind(this));
            }.bind(this));
        }
    }
    private loop() {


        //判断输赢
        if (this.way >= 8) {
            egret.Tween.get(this.ycx1).to({ y: 810 }, 300);
            this.ycx3.alpha = 1;
            this.ycx2.alpha = 0;
            this.floor.y += this.speed;
            this.ycx3.y += this.speed;

            if (this.floor.y >= 0) {
                this.floor.y = 0;
                this.mc.movieClipData = this.mcFactory.generateMovieClipData("ready");
                this.scoreText.text = "WIN!";
                egret.Tween.pauseTweens(this.way1);
                egret.Tween.pauseTweens(this.way2);
                this.removeEventListener(egret.Event.ENTER_FRAME, this.loop, this);
                this.win = true;
                this.dd.play(0, 1)
                this.cheer.play(0, 1);
                egret.Tween.get(this.scoreText).to({ scaleX: 1, scaleY: 1 }, 1000).to({ scaleX: 0, scaleY: 0 }, 1000).call(function () {
                    //ajax提交分数
                    // alert(this.countdownSprite.text)
                    MYajax({ "time": this.countdownSprite.text }, "http://swin.bluej.cn/index.php/Home/app/add_game_achievement.html", function (res) {
                        console.log(res)
                        var ranking = ScenceManage.create(this.stage);
                        ranking.loadScence("preload", this, RankingUI);
                    }.bind(this))

                }.bind(this))
            }


        }
        //计时器
        this.countdownCurNumber += this.frameTime;
        this.countdownSprite.text = this.countdownCurNumber.toFixed(2);
        //移动
        this.way1.y += this.speed;
        this.way2.y += this.speed;
        this.bgBit.y += this.speed;
        this.bgBit2.y += this.speed;
        //循环移动
        if (this.way1.y >= 1136) {
            this.way1.y = 0;
        }
        if (this.way2.y >= 0) {
            this.way2.y = -1136;
            this.way += 1;
        }

        if (this.bgBit.y >= 1136) {
            this.bgBit.y = 0;
        }
        if (this.bgBit2.y >= 0) {
            this.bgBit2.y = -1136;

        }



    }
}